#include "ca51f_config.h"
#include "includes\ca51f2sfr.h"
#include "includes\ca51f2xsfr.h"
#include "includes\gpiodef_f2.h"
#include "includes\system.h"

#include "Library/Includes/delay.h"
#include "Library/Includes/uart.h"
#include "Library/Includes/flash.h"
#include "Library/includes/system_clock.h"
#include <intrins.h>
#include "bsp_lcd.h"
#include "bsp_flash.h"

extern loraAddr_t xdata Myaddr;
extern loraAddr_t xdata Readdr;

int8_t for_each_flasherr(unsigned char *buf, unsigned char length)
{
    unsigned char xdata i,xdata cnt;
    for(i=0;i<length;i++)
    {
        if(buf[i] == 0xFF)
            cnt ++;
        if(cnt == length)
            return -1;
    }
    return 0;
}

void BSP_Flash_Init(void)
{
    unsigned char xdata dat[6];
    PADRD = FLASH_SIZE*4 - 1;  //Set aside 1KB (1024 bytes) for the data area.

    Myaddr.haddr = 0x00;
    Myaddr.laddr = 0x00;
    Myaddr.CH = 0x17;
    Flash_Write_LoraAddr(FLASH_MY_ADDR,&Myaddr);

    // Readdr.Addr = 0x00;
    // Readdr.CH = 0x17;
    // Flash_Write_LoraAddr(FLASH_RE_ADDR,&Readdr);

    // Myaddr.Addr = 0x00;
    // Myaddr.CH = 0x00;
    // Readdr.Addr = 0x00;
    // Readdr.CH = 0x00;

    Data_Area_Mass_Read(0,dat,7);
    
    // 
    // UartPutStr(&dat[1]);
    // UartPutStr(&dat[2]);
    // UartPutStr(&dat[3]);
    // UartPutStr(&dat[4]);
    // UartPutStr(&dat[5]);
    Delay_ms(500);
    if(dat[0] != 1)
    {
        uart_printf("No lora addr data in flash\r\n");
        Myaddr.haddr = 0x00;
        Myaddr.laddr = 0x00;
        Myaddr.CH = 0x17;
        Readdr.haddr = 0x00;
        Readdr.laddr = 0x00;
        Readdr.CH = 0x17;
    }
    else
    {
        Pcdebug("lora addr data in flash\r\n");
        Myaddr.haddr = dat[1];
        Myaddr.laddr = dat[2];
        Myaddr.CH = dat[3];
        Uart0_PutChar(dat[1]);
        Uart0_PutChar(dat[2]);
        Uart0_PutChar(Myaddr.CH);
        Readdr.haddr = dat[4];
        Readdr.laddr = dat[5];
        Readdr.CH = dat[6];
        Uart0_PutChar(dat[4]);
        Uart0_PutChar(dat[5]);
        Uart0_PutChar(Readdr.CH);
    }  
}


/// @brief Write lora address data to flash
/// @param addr flash address
/// @param lora lora data
/// @return 0: successful -1: failed
signed char Flash_Write_LoraAddr(unsigned char addr,loraAddr_t *lora)
{
    uint8_t xdata flag = 1;
    unsigned char xdata dat[7];
    Data_Area_Mass_Read(FLASH_START_ADDR,dat,7);
    dat[0] = flag;
    if(FLASH_MY_ADDR == addr)
    {
        dat[1] = lora->haddr;
        dat[2] = lora->laddr;
        dat[3] = lora->CH;
        Data_Area_Sector_Erase(FLASH_START_ADDR);
        Data_Area_Mass_Write(FLASH_START_ADDR,dat,7);
        Data_Area_Mass_Read(FLASH_MY_ADDR,dat,3);
        // uart_printf("dat = {");
        // for(i = 0; i < 3; i++)
        // {
        //     uart_printf("%d ",dat[i]);
        // }
        // uart_printf("}\r\n");
        if(dat[0] == lora->haddr && dat[1] == lora->laddr && dat[2] == lora->CH)
        {
            return 0;
        }
        else return -1;
    }
    else if(FLASH_RE_ADDR == addr)
    {
        dat[4] = lora->haddr;
        dat[5] = lora->laddr;
        dat[6] = lora->CH;
        Data_Area_Sector_Erase(FLASH_START_ADDR);
        Data_Area_Mass_Write(FLASH_START_ADDR,dat,sizeof(dat));
        Data_Area_Mass_Read(FLASH_RE_ADDR,dat,3);
        if(dat[0] == lora->haddr && dat[1] == lora->laddr && dat[2] == lora->CH)
        {
            return 0;
        }
        else return -1;
    }
    else return -1;
}

/// @brief Read lora address data from flash
/// @param addr flash address
/// @param lora lora data
/// @return 0: successful -1: failed
signed char Flash_Read_LoraAddr(unsigned char addr,loraAddr_t *lora)
{
    unsigned char xdata dat[3];
    if(FLASH_MY_ADDR == addr)
    {
        Data_Area_Mass_Read(FLASH_MY_ADDR,dat,sizeof(dat));
        if(dat[0] != 0xFF && dat[1] != 0xFF && dat[2] != 0xFF)
        {
            lora->haddr = dat[0];
            lora->laddr = dat[1];
            lora->CH = dat[2];
            return 0;
        }
        else return -1;
    }
    else if(FLASH_RE_ADDR == addr)
    {
        Data_Area_Mass_Read(FLASH_RE_ADDR,dat,sizeof(dat));
        if(dat[0] != 0xFF && dat[1] != 0xFF && dat[2] != 0xFF)
        {
            lora->haddr = dat[0];
            lora->laddr = dat[1];
            lora->CH = dat[2];
            return 0;
        }
        else return -1;
    }
    else return -1;
}